package org.fsconnector.cci;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import javax.resource.ResourceException;
import javax.resource.cci.LocalTransaction;

import org.fsconnector.spi.FSManagedConnection;


public class FSConnectionImpl implements FSConnection {

	class FSFileFilter implements java.io.FilenameFilter {
		private FilenameFilter filter;

		public FSFileFilter(FilenameFilter filter) {
			this.filter = filter;
		}

		public boolean accept(File dir, String name) {
			return filter == null ? true : filter.accept(name);
		}
		
		public String toString() {
			return "" + filter;
		}
	}

	private FSManagedConnection manager;

	public FSConnectionImpl(FSManagedConnection manager) {
		this.manager = manager;
	}

	public boolean equals(Object obj) {
		if(!(obj instanceof FSConnectionImpl)) return false;
		return manager.equals(((FSConnectionImpl)obj).manager);
	}
	public void close() {
		manager.close();
	}

	public void copy(FSFile from, FSFile to) throws IOException {
		manager.copy(from.getFile(), to.getFile());
	}

	public void copy(FSFile from, FSFile to, boolean append) throws IOException {
		manager.copy(from.getFile(), to.getFile(), append);
	}

	public FSFile getFile(String filename) {
		return new FSFile(this, manager.getFile(filename), relativePath(filename));
	}

	public FSFile getFile(FSFile parent, String filename) {
		return new FSFile(this, manager.getFile(parent.getFile(), filename),
				parent.getPath().equals("/") ? relativePath(filename) : parent.getPath()
						+ relativePath(filename));
//				(parent.getPath().equals("/") ? "" : parent.getPath())
//				.concat(relativePath(filename)));
	}

	public LocalTransaction getLocalTransaction() throws ResourceException {
		// TODO Auto-generated method stub
		return null;
	}

//	public FSFile[] listFiles() {
////		File[] files = manager.listFiles();
////		FSFile[] fsFiles = new FSFile[files.length];
////		for(int i = 0 ; i < files.length ; ++i) {
////			fsFiles[i] = new FSFile(files[i]);
////		}
////		return fsFiles;
//		return listFiles(null);
//	}
//
//	public FSFile[] listFiles(FilenameFilter filter) {
//		File[] files = manager.listFiles(new FSFileFilter(filter));
//		FSFile[] fsFiles = new FSFile[files.length];
//		for(int i = 0 ; i < files.length ; ++i) {
//			fsFiles[i] = new FSFile(this, files[i], relativePath(files[i].getName()));
//		}
//		return fsFiles;
//	}

	public InputStream openInputStream(String filename) throws FileNotFoundException {
		return manager.openInputStream(filename);
	}

	public InputStream openInputStream(FSFile file) throws FileNotFoundException {
		return manager.openInputStream(file.getFile());
	}

	public OutputStream openOutputStream(String filename) throws IOException {
		return manager.openOutputStream(filename);
	}

	public OutputStream openOutputStream(String filename, boolean append)
			throws IOException {
		return manager.openOutputStream(filename, append);
	}

	public OutputStream openOutputStream(FSFile file) throws IOException {
		return manager.openOutputStream(file.getFile());
	}

	public OutputStream openOutputStream(FSFile file, boolean append) throws IOException {
		return manager.openOutputStream(file.getFile(), append);
	}

	public Reader openReader(String filename) throws FileNotFoundException {
		return manager.openReader(filename);
	}

	public Reader openReader(FSFile file) throws FileNotFoundException {
		return manager.openReader(file.getFile());
	}

	public Writer openWriter(String filename) throws IOException {
		return manager.openWriter(filename);
	}

	public Writer openWriter(String filename, boolean append) throws IOException {
		return manager.openWriter(filename, append);
	}

	public Writer openWriter(FSFile file) throws IOException {
		return manager.openWriter(file.getFile());
	}

	public Writer openWriter(FSFile file, boolean append) throws IOException {
		return manager.openWriter(file.getFile(), append);
	}

	public FSManagedConnection getManager() {
		return manager;
	}

	public void setManager(FSManagedConnection manager) {
		this.manager = manager;
	}

	private String relativePath(String filename) {
		filename = filename.endsWith("/") ? filename.substring(0,filename.length()-1) : filename;
		return filename.startsWith("/") ? filename : "/".concat(filename);
	}

}
